package com.viphuli.fragment;

import android.app.Activity;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.offroader.utils.StringUtils;
import com.offroader.utils.ViewUtils;
import com.viphuli.app.R;
import com.viphuli.common.Constants;
import com.viphuli.common.MyPageHelper;
import com.viphuli.http.ApiRequest;

/**
 * 登录
 * 
 * @author li.li
 *
 */
public class AccountLoginFragment extends BaseProgressFragment implements TextWatcher {

	@Bind(R.id.id_account_login_phone_text)
	protected EditText phoneText;

	@Bind(R.id.id_account_login_vcode_btn)
	protected TextView vcodeBtn;

	@Bind(R.id.id_account_login_vcode_text)
	protected EditText vcodeText;

	@Bind(R.id.id_account_login_btn)
	protected TextView loginBtn;

	@Bind(R.id.id_account_login_agreement)
	protected TextView loginAgreementBtn;

	//是否正在倒计时
	private boolean isCountDown = false;

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_account_login;
	}

	@Override
	protected void initParams() {

	}

	@Override
	protected void initView() {

		phoneText.addTextChangedListener(this);
		vcodeText.addTextChangedListener(this);
		resetButtonView();

	}

	@Override
	protected void initData() {
	}

	@OnClick(R.id.id_account_login_vcode_btn)
	public void getVCode() {
		String phone = phoneText.getText().toString();

		if (StringUtils.isBlank(phone)) {
			Toast.makeText(getActivity(), getString(R.string.tip_input_phone_number), Toast.LENGTH_SHORT).show();
			return;
		}

		if (isCountDown) {
			Toast.makeText(getActivity(), getString(R.string.tip_input_count_down), Toast.LENGTH_SHORT).show();
			return;
		}

		RequestParams params = new RequestParams();
		params.put("phone_number", phone);

		ApiRequest.accountLoginVcode.request(this, params);

	}

	@OnClick(R.id.id_account_login_btn)
	public void login() {

		String phone = phoneText.getText().toString();
		String vCode = vcodeText.getText().toString();

		if (StringUtils.isBlank(phone)) {
			Toast.makeText(getActivity(), getString(R.string.tip_input_phone_number), Toast.LENGTH_SHORT).show();
			return;
		}

		if (StringUtils.isBlank(vCode)) {
			Toast.makeText(getActivity(), getString(R.string.tip_input_vcode), Toast.LENGTH_SHORT).show();
			return;
		}

		RequestParams params = new RequestParams();
		params.put("phone_number", phone);
		params.put("v_code", vCode);

		ApiRequest.accountLogin.request(this, params);

	}

	private void resetButtonView() {
		if (StringUtils.isBlank(phoneText.getText().toString()) || isCountDown) {
			vcodeBtn.setBackgroundResource(R.drawable.bg_common_btn_blue_pressed);
		} else {
			vcodeBtn.setBackgroundResource(R.drawable.bg_common_btn_blue);
		}

		if (StringUtils.isBlank(phoneText.getText().toString()) || StringUtils.isBlank(vcodeText.getText().toString())) {
			loginBtn.setBackgroundResource(R.drawable.bg_common_btn_blue_pressed);
		} else {
			loginBtn.setBackgroundResource(R.drawable.bg_common_btn_blue);
		}
	}

	@OnClick(R.id.id_account_login_agreement)
	public void goToAgreement() {
		ApiRequest.accountUserAgreement.request(getActivity());
	}

	@OnClick(R.id.id_account_login_vcode_voice)
	public void vCodeVoice() {

		if (StringUtils.isBlank(phoneText.getText().toString())) {
			ViewUtils.toast(R.string.tip_input_phone_number);
			return;
		}

		RequestParams params = new RequestParams();
		params.put("phone_number", phoneText.getText().toString());
		ApiRequest.accountLoginVcodeVoice.request(this, params);
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count, int after) {
	}

	@Override
	public void afterTextChanged(Editable s) {
		resetButtonView();
	}

	public TextView getPhoneText() {
		return phoneText;
	}

	public TextView getVcodeBtn() {
		return vcodeBtn;
	}

	public TextView getVcodeText() {
		return vcodeText;
	}

	public TextView getLoginTtn() {
		return loginBtn;
	}

	public TextView getLoginAgreementBtn() {
		return loginAgreementBtn;
	}

	public void setCountDown(boolean isCountDown) {
		this.isCountDown = isCountDown;
	}

	public static void go(Activity act) {
		MyPageHelper.accountLogin.showMyPage(act);
	}

	public static void go(Activity act, int resultCode) {
		MyPageHelper.accountLogin.showMyPage(act, resultCode);
	}

	public static void goWithConfirm(final Activity act) {

		ViewUtils.confirm(act, act.getString(R.string.tip_need_login), new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

				MyPageHelper.accountLogin.showMyPage(act);
			}
		});
	}

	public static void goWithConfirm(final Activity act, String msg) {

		ViewUtils.confirm(act, msg, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

				MyPageHelper.accountLogin.showMyPage(act);
			}
		});
	}

	public static void goWithConfirmResult(final Activity act, String msg) {

		ViewUtils.confirm(act, msg, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

				MyPageHelper.accountLogin.showMyPage(act, Constants.ACTIVITY_REQUEST_REFREASH_CODE);
			}
		});
	}

}
